package org.ceteca.explica.client.gui.common;

import java.util.ArrayList;
import java.util.HashSet;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.ExplicaDateSelect;
import org.ceteca.explica.client.gui.ExplicaDetailEntityView;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.core.InterfaceErrors;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.PriceListLineOT;
import org.ceteca.explica.core.common.PriceListOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.javaglade.InterfaceGladeGtk;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY <br>
 *         without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 *         PARTICULAR PURPOSE. See the GNU General Public License for more
 *         details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class PriceListClient extends CommonClient {
	// WIDGETS
	private Text entryPriceListCode, entryPriceListDescription;

	private Text w_entryPriceListFromDate, w_entryPriceListToDate;

	private Button buttonPriceListTaxesInc, w_selectFromDate, w_selectToDate;

	private Composite panelLista;

	private ExplicaDateSelect selectFromDate, selectToDate;

	public PriceListClient() {
		super(PriceListOT.class,
				InterfaceExplica.EXPLICA_EDIT_PRICELIST_GLADE_PATH,
				InterfaceExplica.EDIT_PRICELILST_DIALOG_NAME);
	}

	@Override
	public BaseVO getEntityData() {
		PriceListOT entity = new PriceListOT();
		if (this.entEditView != null) {
			this.logger.debug("Getting entity data from widgets");
			this.getWidgets();
			entity.setDescription(entryPriceListDescription.getText());

			entity.setFromDate(Converter.getInstance().parseDate(
					w_entryPriceListFromDate.getText()));
			entity.setToDate(Converter.getInstance().parseDate(
					w_entryPriceListToDate.getText()));
			entity.setTaxesInc(buttonPriceListTaxesInc.getSelection());

			if (entity.getLines() == null)
				entity.setLines(new HashSet<PriceListLineOT>());

			if (!isDetail()) {
				String subentityId = InterfaceExplica.EXPLICA_ENTITY_PRICELISTLINE_ID;
				entity.getLines().clear();

				ExplicaDetailEntityView view = this.entEditView
						.getMultiSelectEntityView(subentityId);
				entity.getLines().addAll(view.getRegistries());
			}

		}

		if (selectedEntity != null) {
			entity.setVersion(((PriceListOT) this.selectedEntity).getVersion());
			entity.setIdPriceList(((PriceListOT) this.selectedEntity)
					.getIdPriceList());
		}

		return entity;
	}

	@Override
	public OperationResult validateEntity() {
		OperationResult result = new OperationResult();
		boolean datosValidos = true;
		PriceListOT entity = (PriceListOT) getEntityData();

		if (datosValidos)
			result.addResultMessage(OperationResult.RESULT_OK, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfaceErrors.MSG_VALIDATE_OK));
		else
			result.addResultMessage(OperationResult.RESULT_ERROR, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfaceErrors.MSG_VALIDATE_ERROR));
		return result;
	}

	public void getWidgets() {
		ArrayList<Integer> codError = new ArrayList<Integer>();

		if ((entryPriceListCode = (Text) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_CODE_WIDGETNAME)) == null)
			codError.add(1);

		if ((entryPriceListDescription = ((Text) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_DESCRIPTION_WIDGETNAME))) == null)
			codError.add(2);

		if ((w_entryPriceListFromDate = (Text) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_FROMDATE_WIDGETNAME)) == null)
			codError.add(3);
		if ((w_entryPriceListToDate = (Text) this
				.getWidget(InterfaceExplica.EDIT_PRICELILST_TODATE_WIDGETNAME)) == null)
			codError.add(4);
		if ((buttonPriceListTaxesInc = (Button) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_TAXEXINC_WIDGETNAME)) == null)
			codError.add(5);

		if ((panelLista = (Composite) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_PRICELISTLINE_PANEL_WIDGETNAME)) == null)
			codError.add(6);

		if ((w_selectFromDate = (Button) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_BUTTONSELECTFROMDATE_WIDGETNAME)) == null)
			codError.add(7);

		if ((w_selectToDate = (Button) this
				.getWidget(InterfaceExplica.EDIT_PRICELIST_BUTTONSELECTTODATE_WIDGETNAME)) == null)
			codError.add(8);

		if (codError.size() > 0) {
			System.out.println("Error obteniendo widgets ");
			for (int cod:codError)
				System.out.println(cod);
			fireExceptionAndExit("Error obteniendo widget");
		}
	}

	public void setWidgetsValues(BaseVO entity) {
		PriceListOT ent = (PriceListOT) entity;
		getWidgets();
		entryPriceListCode.setText(Converter.getInstance().toString(
				ent.getIdPriceList()));
		entryPriceListDescription.setText(ent.getDescription());

		w_entryPriceListFromDate.setText(Converter.getInstance().toString(
				ent.getFromDate()));

		w_entryPriceListToDate.setText(Converter.getInstance().toString(
				ent.getToDate()));

		buttonPriceListTaxesInc.setSelection(ent.isTaxesInc());

		ExplicaDetailEntityView view = this.entEditView
				.getMultiSelectEntityView(InterfaceExplica.EXPLICA_ENTITY_PRICELISTLINE_ID);
		if (view != null) {
			view.setRegistries(((PriceListOT) this.selectedEntity).getLines());
		}
	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryPriceListCode.setEnabled(false);
		entryPriceListDescription.setEnabled(enabled);
		w_entryPriceListFromDate.setEnabled(enabled);
		w_entryPriceListToDate.setEnabled(enabled);
		buttonPriceListTaxesInc.setEnabled(enabled);
		w_selectFromDate.setEnabled(enabled);
		w_selectToDate.setEnabled(enabled);
		panelLista.setEnabled(enabled);
	}

	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();

		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();
		this.logger.debug("Grabando widget:" + panelLista);
		lMultiSelectionSubentities
				.add(new MultiSelectionSubentityOT(
						InterfaceExplica.EXPLICA_ENTITY_PRICELISTLINE_ID,
						"priceList",
						this,
						this
								.getSwtWidget(InterfaceExplica.EDIT_PRICELIST_PRICELISTLINE_PANEL_WIDGETNAME),
						"lines", null));
	}

	@Override
	public void init_specific_components() {
		getWidgets();
		ExplicaGUIBuilder.getInstance().connectSignal(
				InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, w_selectFromDate,
				"on_select_from_date_clicked", this);
		ExplicaGUIBuilder.getInstance().connectSignal(
				InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, w_selectToDate,
				"on_select_to_date_clicked", this);

	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub

	}

	@Override
	public void fillEntityData() {
		PriceListOT ent = (PriceListOT) this.getSelectedEntity();
		this.setWidgetsValues(ent);
	}

	public void on_select_from_date_clicked(Event e) {
		selectFromDate = new ExplicaDateSelect(this.logger, this, "on_from_date_selected");
		selectFromDate.build();
		selectFromDate.show(Converter.getInstance().parseDate(
				w_entryPriceListFromDate.getText()));

	}

	public void on_select_to_date_clicked(Event e) {
		selectToDate = new ExplicaDateSelect(this.logger, this, "on_to_date_selected");
		selectToDate.build();
		selectToDate.show(Converter.getInstance().parseDate(
				w_entryPriceListToDate.getText()));

	}
	public void on_from_date_selected(Event e) {

		w_entryPriceListFromDate.setText(Converter.getInstance().toString(selectFromDate.getDate()));

}
	
	public void on_to_date_selected(Event e) {

			w_entryPriceListToDate.setText(Converter.getInstance().toString(selectToDate.getDate()));

	}

}
